package com.woniuxy.controller;

import com.alibaba.excel.EasyExcel;
import com.woniuxy.entity.Merchants;
import com.woniuxy.entity.QueryEntity;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.MerchantsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.List;

/**
 * @author Leimin
 * @date 2022/04/26 19:09
 **/
@RestController
@RequestMapping("/merchants")
@Api(tags = "客商接口")
public class MerchantsController {
    @Autowired
    MerchantsService merchantsService;

    @PostMapping("/condition")
    @ApiOperation(value = "条件查询客商分页数据")
    public ResponseEntity findAllByPage(@ApiParam(name = "queryEntity", value="查询条件") @RequestBody QueryEntity<Merchants> queryEntity){
        return merchantsService.findAllByCondition(queryEntity);
    }

    @GetMapping("/merchants")
    @ApiOperation(value = "获取所有客商数据")
    public ResponseEntity findAll(){

        return merchantsService.findAll();
    }

    @GetMapping("/merchants/{id}")
    @ApiOperation(value = "获取一个客商数据")
    public ResponseEntity findOne(@ApiParam(name = "id", value="客商id") @PathVariable int id){

        return merchantsService.findOne(id);
    }

    @PostMapping("/merchants")
    @ApiOperation(value = "新增客商")
    public ResponseEntity addOne(@ApiParam(name = "merchants", value="新增的客商") @Valid @RequestBody Merchants merchants){

        return merchantsService.addOne(merchants);
    }

    @DeleteMapping("/merchants/{id}")
    @ApiOperation(value = "删除客商")
    public ResponseEntity deleteOne(@ApiParam(name = "id", value="客商id") @PathVariable int id){

        return merchantsService.deleteOne(id);
    }

    @PutMapping("/merchants/batchDel")
    @ApiOperation(value = "批量删除客商")
    public ResponseEntity deleteBatch(@ApiParam(name = "ids", value="客商id数组")@RequestBody int[] ids){

        return merchantsService.deleteBatch(ids);
    }

    @PutMapping("/merchants")
    @ApiOperation(value = "更新客商")
    public ResponseEntity updateOne(@ApiParam(name = "merchants", value="更新的客商")@Valid @RequestBody Merchants merchants){

        return merchantsService.updateOne(merchants);
    }

    @PostMapping("/finance")
    @ApiOperation(value = "条件查询财务分页数据")
    public ResponseEntity findFinanceByPage(@ApiParam(name = "queryEntity", value="查询条件")@RequestBody QueryEntity<Merchants> queryEntity){

        return merchantsService.findFinance(queryEntity);
    }

    @GetMapping("/select")
    @ApiOperation(value = "获取下拉框数据")
    public ResponseEntity findSelectOption(){
        return merchantsService.findSelectOption();
    }

    @GetMapping("/code/{code}")
    @ApiOperation(value = "根据编码查询客商数据")
    public ResponseEntity findByCode(@ApiParam(name = "code", value="客商编码")@PathVariable String code){

        return merchantsService.findByCode(code);
    }

    @GetMapping("/name/{name}")
    @ApiOperation(value = "根据名称查询客商数据")
    public ResponseEntity findByName(@ApiParam(name = "name", value="客商名称")@PathVariable String name){
        String newName=null;
        try {
           newName= URLDecoder.decode(name,"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return merchantsService.findByName(newName);
    }

    @PostMapping("/import")
    @ApiOperation(value = "从excel导入")
    public ResponseEntity importExcel(@ApiParam(name = "file", value="excel文件")@RequestPart("file") MultipartFile file) {
        List<Merchants> list = null;
        try {
            list = EasyExcel.read(file.getInputStream())
                    .head(Merchants.class)
                    .sheet()
                    .doReadSync();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return merchantsService.importExcel(list);
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出excel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        // 这里URLEncoder.encode可以防止中文乱码，所有通过后端的文件下载都可以如此处理
        String fileName = URLEncoder.encode("客商列表.xlsx", "UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf8");
        //建议加上该段，否则可能会出现前端无法获取Content-disposition
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName );
        EasyExcel.write(response.getOutputStream(), Merchants.class).sheet("模板")
                .doWrite((Collection<?>) merchantsService.findAll().getData());
    }
}

